﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 剧情设计器2._0
{
    public partial class PlotEditor : Form
    {
        Plot plot;
        public PlotEditor(Plot p)
        {
            plot = p;
            InitializeComponent();
        }

        void UpdateDisplay()
        {
            edit_plotname.Text = plot.name;
            edit_timeoffset.Text = plot.time_offset.ToString();
            listBox_events.BeginUpdate();
            listBox_events.Items.Clear();
            foreach(Event e in plot.events)
            {
                listBox_events.Items.Add(e.name);
            }
            listBox_events.EndUpdate();
        }

        private void PlotEditor_Load(object sender, EventArgs e)
        {
            UpdateDisplay();
        }

        private void btn_create_Click(object sender, EventArgs e)
        {
            plot.events.Add(new Event() { name = "新事件" });
            UpdateDisplay();
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            int idx = listBox_events.SelectedIndex;
            if (idx < 0) return;
            if(MessageBox.Show(string.Format("确定要删除事件 {0} 吗？", plot.events[idx].name), "确定删除", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                plot.events.RemoveAt(idx);
                UpdateDisplay();
            }
        }

        private void listBox_events_SelectedIndexChanged(object sender, EventArgs e)
        {
            int idx = listBox_events.SelectedIndex;
            int max = plot.events.Count - 1;
            int min = 0;
            if(idx == min && idx == max)
            {
                btn_moveUp.Enabled = false;
                btn_moveDown.Enabled = false;
            }
            else if(idx == max)
            {
                btn_moveUp.Enabled = false;
                btn_moveDown.Enabled = true;
            }
            else if(idx == min)
            {
                btn_moveUp.Enabled = true;
                btn_moveDown.Enabled = false;
            }
            else
            {
                btn_moveUp.Enabled = true;
                btn_moveDown.Enabled = true;
            }
        }

        private void edit_plotname_TextChanged(object sender, EventArgs e)
        {
            plot.name = edit_plotname.Text;
        }

        private void btn_moveUp_Click(object sender, EventArgs e)
        {
            int idx = listBox_events.SelectedIndex;
            Event temp = plot.events[idx];
            plot.events[idx] = plot.events[idx - 1];
            plot.events[idx - 1] = temp;
            UpdateDisplay();
        }

        private void btn_moveDown_Click(object sender, EventArgs e)
        {
            int idx = listBox_events.SelectedIndex;
            Event temp = plot.events[idx];
            plot.events[idx] = plot.events[idx + 1];
            plot.events[idx + 1] = temp;
            UpdateDisplay();
        }

        private void listBox_events_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int idx = listBox_events.SelectedIndex;
            if (idx < 0) return;
            EventEditor ee = new EventEditor(plot.events[idx]);
            ee.Show();
        }

        private void PlotEditor_FormClosed(object sender, FormClosedEventArgs e)
        {
            Program.mainform.UpdateDisplay();
        }

        private void edit_timeoffset_TextChanged(object sender, EventArgs e)
        {
            try
            {
                plot.time_offset = int.Parse(edit_timeoffset.Text);
            }
            catch(FormatException)
            {
                //MessageBox.Show("请输入一个整数", "输入异常");
            }
        }
    }
}
